@extends('layouts.admin_common') @section('content')

    @if(app('env')=='production')
        <script src="{{ secure_asset('/js/sy_common.js') }}"></script>
    @else
        <script src="{{ asset('/js/sy_common.js') }}"></script>
    @endif
    <div class="breadcrumbs_container">{{Breadcrumbs::render('admin.buy') }}</div>
    <div class="system_tab_line"></div>
    <div class="lyx_alert"> </div>
    <div class="lyx_search_table">

        <div class="row">
            <div class="col-sm-3">
                <button class="btn btn-primary button_normal" id="lyx_add_line">添加商品</button>
            </div>
        </div>

        <div class="lyx_table">

            <table class="table table-bordered">
                <thead>
                <tr>
                    <th scope="col">@lang('symessages.info.category')</th>
                    <th scope="col">@lang('symessages.info.product_code')</th>
                    <th scope="col">@lang('symessages.info.product_name')</th>
                    <th scope="col">@lang('symessages.info.store_num')</th>
                    <th scope="col">@lang('symessages.info.subscribe_num')</th>
                    <th scope="col">@lang('symessages.info.price')</th>
                    <th scope="col">@lang('symessages.info.size')</th>
                    <th scope="col">@lang('symessages.info.subscribe_date')</th>
                    <th scope="col">@lang('symessages.info.in_num')</th>
                    <th scope="col">@lang('symessages.info.total_money')</th>
                    <th scope="col">備考</th>
                </tr>
                </thead>
                <tbody id="lyx_tbody">
                <tr>
                    <td>
                        <select class="form-control form-control-sm lyx_cat_select">
                            <option>select category</option>
                            @foreach($category as $cat)
                                <option value="{{$cat->category_id}}">{{$cat->category_name}}</option>
                            @endforeach
                        </select>
                    </td>
                    <td>
                        before select category
                    </td>
                    <td class="product_name"></td>
                    <td class="store_num"></td>
                    <td class="subscribe_num"></td>
                    <td class="price"></td>
                    <td class="lyx_size_arr"></td>
                    <td class="end_date">
                        <input class="form-control form-control-sm lyx_subscribe_date" type="text">
                    </td>
                    <td width="100px">
                        <input class="form-control form-control-sm lyx_house_num" type="number" placeholder="">
                    </td>
                    <td></td>
                    <td>{{$order_id}}</td>
                </tr>

                <tr id="lyx_total_tr">
                    <td colspan="9" style="text-align: center">合計金額</td>
                    <td colspan="2" id="total_sum_money">¥0</td>
                </tr>

                </tbody>
            </table>

        </div>
        <div class="row">
            <div class="col-sm-2"></div>
            <div class="col-sm-3">
                    <button class="btn btn-primary button_normal" id="lyx_csv_upload">Csv取込</button>
            </div>
            <div class="col-sm-3">
                <button class="btn btn-primary button_normal" id="lyx_post_submit">确定</button>
            </div>
            <div class="col-sm-3">
                <button class="btn btn-primary button_normal">WEB反映</button>
            </div>
        </div>
    </div>


<script>
    $(function () {

        var postLineData = [];
        var total_sum_money = 0;//总金额
        var order_id = '{{$order_id}}';
        add_select_action();

        $('#lyx_post_submit').on('click',function () {
            postData();
        })

        //--添加按钮
        $('#lyx_add_line').on('click', function () {
            addLine();
        })

        /**
         * 添加一条记录
         */
        function addLine() {
            var html = ['<tr>',
                '                    <td>',
                '                        <select class="form-control form-control-sm lyx_cat_select">',
                '                            <option>select category</option>',
                @foreach($category as $cat)
                    '                                <option value="{{$cat->category_id}}">{{$cat->category_name}}</option>',
                @endforeach
                    '                        </select>',
                '                    </td>',
                '                    <td>',
                '                        before select category',
                '                    </td>',
                '                    <td class="product_name"></td>',
                '                    <td class="store_num"></td>',
                '                    <td class="subscribe_num"></td>',
                '                    <td class="price"></td>',
                '                    <td class="lyx_size_arr"></td>',
                '                    <td class="end_date">',
                '                    <input class="form-control form-control-sm lyx_subscribe_date" type="text">',
                '                    </td>',
                '                    <td width="100px">',
                '                        <input class="form-control form-control-sm lyx_house_num" type="text" placeholder="">',
                '                    </td>',
                '                    <td></td>',
                '                    <td>'+order_id+'</td>',
                '                </tr>'].join("");
            $('#lyx_total_tr').before(html);
            add_select_action();
        }

        //-选择分类
        function add_select_action() {
            $(".lyx_cat_select").off().change(function () {
                var value = $(this).val();
                var that = $(this).parent('td').next('td');
                if (value == null || value == '' || value == undefined) {
                    that.html('select category');
                } else {
                    that.html('loading...');
                    useCatgetGoods($(this).val(), that);
                }
            });
        }

        /**
         *
         * @param cat_id 产品类别id
         * @param that 操作哪个dom
         */
        function useCatgetGoods(cat_id, that) {
            $.ajax({
                url: '/admin/use_catget_goods',
                type: 'post',
                data: {cat_id: cat_id},
                success: function (retData) {
                    var html1 = '<select class="form-control form-control-sm lyx_goods_select"><option value="">select goods</option>';
                    var html3 = '</select>';
                    var html2 = '';
                    if (retData.data.length > 0) {
                        $.each(retData.data, function (index, item) {
                            html2 = html2 + '<option value="' + item.goods_id + '">' + item.goods_code + '</option>';
                        })
                        var html_end = html1 + html2 + html3;
                    } else {
                        html_end = 'empty!'
                    }
                    $(that).html(html_end);
                    add_goods_select_action();
                }
            })
//            lyxAlert('alert-primary', '你好', 3000);
        }

        //-选择产品
        function add_goods_select_action() {
            $(".lyx_goods_select").off().change(function () {
                var value = $(this).val();
                var that = $(this).parent('td').next('td');
                if (value == null || value == '' || value == undefined) {
                    that.html('select goods');
                } else {
                    that.html('loading...');
                    getGoodsInfo($(this).val(), that);
                }
            });
        }

        function getGoodsInfo(goods_id, that) {
            $.ajax({
                url: '/admin/get_goods_info',
                type: 'post',
                data: {goods_id: goods_id,subscribe:1},
                success: function (retData) {
                    var two = that.next();
                    var three = two.nextAll('.price');
                    var fore = two.nextAll('.subscribe_num');
                    that.html(retData.data.goods_name);
                    two.html(retData.data.total_num);
                    three.html('¥' + retData.data.price);
                    fore.html(retData.data.subscribe);
                    three.data('price', retData.data.price);
                    add_goods_num_action();
                    getSizeArr(goods_id, that);
                }
            })
//            lyxAlert('alert-primary', '你好', 3000);
        }

        //--添加数量
        function add_goods_num_action() {
            $(".lyx_house_num").off().change(function () {
                var num = $(this).val();
                var that = $(this).parent('td').next('td');
                var price = $(this).parents('tr').find('.price').data('price');
                var total_money = num * price;
                that.html('¥' + total_money);
                that.data('totalMoney', total_money);
                var thatTr = $(this).parents('tr');
                addDataToAry(thatTr, num);
            });
        }

        /**
         * 添加数据到数组里面
         */
        function addDataToAry(that, num) {
            var category_id = that.find('.lyx_cat_select').val();
            var goods_id = that.find('.lyx_goods_select').val();
            var goods_name = that.find('td').eq(2).html();
            var price = that.find('td').eq(5).data('price');
            var subscribe_date = that.find(('.lyx_subscribe_date')).val();
            console.log(category_id, goods_id, goods_name, price);
            var json = {
                category_id: category_id,
                goods_id: goods_id,
                goods_name: goods_name,
                price: price,
                goods_num:num,
                total_money: num * price,
                subscribe_date:subscribe_date
            };
            that.data('onedata', json);
            sumToalMoney();
        }

        /**
         * 尺寸数据
         */
        function getSizeArr(id, that) {
            $.ajax({
                url: '/admin/get_model_info',
                type: 'post',
                data: {goods_id: id},
                success: function (retData) {
                    var theme = that.parents('tr');
                    var html = size_html(retData.data);
                    theme.find('.lyx_size_arr').html(html);
                    theme.data('model_info',retData.data[0]);
                    add_size_action();
                }
            })
        }

        /**
         * 尺寸的代码片段
         */
        function size_html(data) {
            var html_1 = '<select class="form-control form-control-sm lyx_size_select">';
            var html_2 = ''
            var html_3 = '</select>';
            $.each(data, function (index, item) {
                html_2 += "<option value='" + JSON.stringify(item) + "'>" + item.color_name+"/"+item.size_name + "</option>";
            })
            return html_1 + html_2 + html_3;
        }

        function add_size_action() {
            $(".lyx_size_select").off().change(function () {
                var value = $(this).val();
                console.log(value);
                $(this).parents('tr').data('model_info',eval('(' + value + ')'));
                sumToalMoney();
            });
        }

        /**
         * 计算总价，并且把数据放到数据中
         */
        function sumToalMoney() {
            postLineData = [];
            $('tr').each(function () {
                var j = $(this).data('onedata');
                if (j != undefined && j != null && j != '') {
                    total_sum_money = total_sum_money + j.total_money;
                    var modelInfo = $(this).data('model_info');
                    j.model_info = modelInfo;
                    postLineData.push(j);
                }
            })
            $('#total_sum_money').html('¥' + total_sum_money);
        }

        function postData() {

            $.ajax({
                url:'save_subscribe_order',
                type:'post',
                data:{
                    order_id:order_id,
                    goods_list:postLineData
                },
                success:function (retData) {
                    if(retData.code == 1001){
                        successAlert();
                    }else{
                        lyxAlert('alert-primary', retData.msg, 3000);
                    }
                },
                error:function (retData) {
                    var msg = [];
                    $.each(retData.responseJSON.errors,function (index,item) {
                        msg.push(item[0]);
                    })
                    if(!msg[0]){
                        msg[0] = retData.responseJSON.message;
                    }
                    lyxAlert('alert-primary', msg[0], 3000);
                }
            })

        }




        $('#lyx_csv_upload').after('<input type="file" id="load_xls" name="csvFile" style="display:none" onchange ="uploadFile()">');
        $('#lyx_csv_upload').click(function(){
            document.getElementById("load_xls").click();
        });


    })


    function uploadFile() {
        var myform = new FormData();
        myform.append('file',$('#load_xls')[0].files[0]);
        $.ajax({
            url: "upload_csv",
            type: "POST",
            data: myform,
            contentType: false,
            processData: false,
            success: function (data) {
                console.log(data);
            },
            error:function(data){
                console.log(data)
            }
        });
    }
</script>
@endsection

